package com.meadvc.ssmdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.meadvc.ssmdemo.service.ComponentService;
import com.meadvc.ssmdemo.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: ssm-demo
 * @description: 联系人控制类
 * @author: meadvc
 * @create: 2020-06-15 21:51
 **/
@RestController
@RequestMapping("/cont")
public class ContactsController {

    @Autowired
    private ContactService service;

    /**
     * 查询联系人列表
     */
    @GetMapping("/listCont")
    @CrossOrigin
    public JSONObject listContacts(HttpServletRequest request) {
        String query = request.getParameter("query");
        int pagenum = Integer.parseInt(request.getParameter("pagenum"));
        int pagesize = Integer.parseInt(request.getParameter("pagesize"));
        //System.out.println(query+"--"+pagenum+"--"+pagesize);
        return service.listContacts(query,pagenum,pagesize);
    }

    /**
     * 添加联系人
     * @param requestJson
     * @return
     */
    @PostMapping("addContact")
    @CrossOrigin
    public JSONObject addContact(@RequestBody JSONObject requestJson) {
        return service.addContact(requestJson);
    }

    /**
     * 根据id获取联系人信息
     * @param request
     * @return
     */
    @GetMapping("/getContact")
    @CrossOrigin
    public JSONObject getContact(HttpServletRequest request) {
        int tid = Integer.parseInt(request.getParameter("tid"));
        return service.getContact(tid);
    }
    @PostMapping("updateContact")
    @CrossOrigin
    public JSONObject updateContact(@RequestBody JSONObject requestJson) {
        return service.updateContact(requestJson);
    }

    @PostMapping("deleteContact")
    @CrossOrigin
    public JSONObject deleteContact(@RequestBody JSONObject requestJson) {
        return service.deleteContact(requestJson);
    }



}
